﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace INTEX.SCHEDULER
{
    public partial class JMRULES
    {
        public DataTable TBSTDM;
        public DataTable srcSRULES;
        Dictionary<string, DataTable> _MRules ;
        Dictionary<string, DataTable> MRules
        {
            get
            {
                if (_MRules == null) PrepareMachineRules();
                return _MRules;
            }
        }
        public JMRULES()
        { }
        public double RGroupValidate(DataRow[] Rules,  DataRow dwJob)
        {
            double rgPoints = 0;
            foreach (DataRow rule in Rules)
            {
                string logic = rule["LOGIC"].ToString();
                string JOBV = rule["JOBV"].ToString();
                string RVALUE = rule["RVALUE"].ToString();
                double POINTS = Convert.ToDouble(rule["POINTS"].ToString());
                string value = dwJob[JOBV].ToString();

                switch (logic)
                {
                    case "=":
                        if (value.Trim() == RVALUE.Trim())
                        {
                            rule["JMVALUE"] = POINTS;
                            rgPoints += POINTS;
                        }
                        else
                        {
                            rule["JMVALUE"] = 0;
                        }
                        break;
                    case ">":
                        if (Convert.ToDecimal(value) > Convert.ToDecimal(RVALUE))
                        {
                            rule["JMVALUE"] = POINTS;
                            rgPoints += POINTS;
                        }
                        else
                        {
                            rule["JMVALUE"] = 0;
                        }
                        break;
                    case ">=":
                        if (Convert.ToDecimal(value) >= Convert.ToDecimal(RVALUE))
                        {
                            rule["JMVALUE"] = POINTS;
                            rgPoints += POINTS;
                        }
                        else
                        {
                            rule["JMVALUE"] = 0;
                        }
                        break;
                    case "<":
                        if (Convert.ToDecimal(value) < Convert.ToDecimal(RVALUE))
                        {
                            rule["JMVALUE"] = POINTS;
                            rgPoints += POINTS;
                        }
                        else
                        {
                            rule["JMVALUE"] = 0;
                        }
                        break;
                    case "<=":
                        if (Convert.ToDecimal(value) <= Convert.ToDecimal(RVALUE))
                        {
                            rule["JMVALUE"] = POINTS;
                            rgPoints += POINTS;
                        }
                        else
                        {
                            rule["JMVALUE"] = 0;
                        }
                        break;
                    case "<>":
                        if (value != RVALUE)
                        {
                            rule["JMVALUE"] = POINTS;
                            rgPoints += POINTS;
                        }
                        else
                        {
                            rule["JMVALUE"] = 0;
                        }
                        break;
                    default:
                        break;
                }
            }
            return rgPoints;
        }
        public void RValidate(DataRow dwMachine, DataRow dwJob)
        {
            DataTable Rules = null;
            string MACHINEID = dwMachine["MACHINEID"].ToString().Trim();
            dwMachine["JMVALUE"] = 0;
            if (MRules.ContainsKey(MACHINEID))
                Rules = MRules[MACHINEID];
            string CurrentRGroup = "";
            double rPoint = 0;
            double mPoints = 0;
            bool IsFond = false;
            var qRule = from rule in Rules.AsEnumerable()
                        where rule["MACHINEID"].ToString().Trim() == MACHINEID
                        group rule by new
                          {
                              PT = rule["RGROUP"].ToString(),
                          }
                            into g
                            select new
                            {
                                KEY = g.Key,
                            };
            foreach (var Grule in qRule)
            {
                IsFond = true;
                string Group = Grule.KEY.PT;
                string filter = "RGROUP ='" + Group + "'";
                string sortOrder = "RGROUP ASC,RULEID ASC";
                DataRow[] rows = Rules.Select(filter, sortOrder);
                rPoint = RGroupValidate(rows, dwJob);
                mPoints += rPoint;
                dwMachine["JMVALUE"] = mPoints;
            }
            if (!IsFond)
            {
                dwMachine["JMVALUE"] = 100;
            }
           
        }

        public void PrepareMachineRules()
        {
            _MRules = new Dictionary<string, DataTable>();
            foreach (DataRow dwMachine in TBSTDM.Rows)
            {
                string MACHINEID = dwMachine["MACHINEID"].ToString().Trim();
                DataTable Rules = new DataTable();
                Rules.TableName = "RULES";
                if (MRules.ContainsKey(MACHINEID))
                    continue;
                string filter = " TRIM(MACHINEID)= '" + MACHINEID + "'";
                string sort = "RGROUP ASC,RULEID ASC";
                DataRow[] machinetypes = srcSRULES.Select(filter, sort);
                if (machinetypes.Length > 0)
                {
                    Rules = machinetypes.CopyToDataTable();
                }
                MRules.Add(MACHINEID, Rules);
            }
        }
    }
}
